from django.shortcuts import render
from rest_framework.generics import ListAPIView, RetrieveAPIView
from course.models import CourseDirectionModel, CourseCategoryModel, CourseModel, CourseChapterModel
from course.serializers import CourseDirectionModelSerializer, CourseCategoryModelSerializer, CourseModelSerializer, CourseRetrieveSerializer, CourseChapterSerializer
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination
from drf_haystack.viewsets import HaystackViewSet
from drf_haystack.filters import HaystackFilter
from course.serializers import CourseIndexHaystackSerializer
from course.models import CourseModel


class CoursePageNumberPagination(PageNumberPagination):
    page_size = 5
    page_query_param = 'page'
    page_size_query_param = 'size'
    max_page_size = 10


# 课程一级分类：方向视图
class CourseDirectionListApiView(ListAPIView):
    queryset = CourseDirectionModel.objects.filter(is_show=True, is_deleted=False).order_by('-id')
    serializer_class = CourseDirectionModelSerializer


# id
# 二级分类：课程分类视图
class CourseCategoryListApiView(ListAPIView):
    serializer_class = CourseCategoryModelSerializer
    # queryset = CourseCategoryModel.objects.filter(is_show=True,is_deleted=False).order_by('-id')

    def get_queryset(self):
        print('-----------')
        print(self.kwargs)
        direction_id = int(self.kwargs.get('directionID', 0))
        query_set = CourseCategoryModel.objects.filter(is_show=True, is_deleted=False).order_by('-id')
        if direction_id > 0:
            query_set = query_set.filter(direction=direction_id)
        return query_set


# 课程列表视图
class CourseListApiView(ListAPIView):
    serializer_class = CourseModelSerializer
    filter_backends = [OrderingFilter]  # 局部指定过滤器
    ordering_fields = ['id', 'price', 'students']
    pagination_class = CoursePageNumberPagination

    # queryset = 结果集
    def get_queryset(self):
        direction_id = int(self.kwargs.get('directionID', 0))
        category_id = int(self.kwargs.get('categoryID', 0))

        queryset = CourseModel.objects.filter(is_show=True, is_deleted=False).order_by('id')
        if direction_id > 0:
            queryset = queryset.filter(direction=direction_id)

        if category_id > 0:
            queryset = queryset.filter(category=category_id)

        return queryset


# 课程详情视图
class CourseDetaileView(RetrieveAPIView):
    queryset = CourseModel.objects.all()
    lookup_field = 'pk'
    lookup_url_kwarg = 'courseID'
    serializer_class = CourseRetrieveSerializer


# 根据课程id,获取章节列表
class CourseChapterListApiView(ListAPIView):
    queryset = CourseChapterModel.objects.all()
    serializer_class = CourseChapterSerializer
    lookup_field = 'pk'
    lookup_url_kwarg = 'courseID'


# elasticsearch搜索
class CourseSearchViewSet(HaystackViewSet):
    """课程信息全文搜索视图类"""
    # 指定本次搜索的最终真实数据的保存模型
    index_models = [CourseModel]
    serializer_class = CourseIndexHaystackSerializer
    filter_backends = [OrderingFilter, HaystackFilter]
    ordering_fields = ('id', 'students', 'orders')
    pagination_class = CoursePageNumberPagination
